package com.jingtuo.android.fragment;

import java.util.ArrayList;
import java.util.List;

import com.jingtuo.android.R;
import com.jingtuo.android.common.app.BaseFragment;
import com.jingtuo.android.common.widget.ListAdapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;


public class NavigationFragment extends BaseFragment implements OnItemClickListener{
	
	public static final String KEY_TITLES = "titles";
	
	protected ArrayList<String> data;
	
	private ListView listView;
	
	private Adapter adapter;
	
	private OnNavgationFragmentListener onNavgationFragmentListener;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Bundle arguments = getArguments();
		if(arguments!=null){
			data = arguments.getStringArrayList(KEY_TITLES);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		listView = (ListView)inflater.inflate(R.layout.navigation, container, false);
		adapter = new Adapter(getActivity(), data);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		return listView;
	}
	
	
	class Adapter extends ListAdapter<String> {

		public Adapter(Context context, List<String> data) {
			super(context, data);
			// TODO Auto-generated constructor stub
		}
		
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(convertView==null){
				convertView = View.inflate(context, R.layout.navigation_item, null);
			}
			TextView textView = (TextView)convertView;
			textView.setText(data.get(position));
			return convertView;
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		if(onNavgationFragmentListener!=null){
			onNavgationFragmentListener.onClick(this, data.get(position));
		}
	}
	
	public interface OnNavgationFragmentListener{
		public void onClick(NavigationFragment nf, String title);
	}

	public void setOnNavgationFragmentListener(
			OnNavgationFragmentListener onNavgationFragmentListener) {
		this.onNavgationFragmentListener = onNavgationFragmentListener;
	}
	
}
